<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var insert = function (intervals, newInterval) {
      intervals.push(newInterval);
      intervals.sort((a, b) => a[0] - b[0]);
      let res = [intervals[0]];
      for (let k = 0; k < intervals.length; k++) {
        if (res[res.length - 1][1] >= intervals[k][0]) {
          if (res[res.length - 1][1] <= intervals[k][1]) {
            res[res.length - 1][1] = intervals[k][1]
          }
        } else {
          res.push(intervals[k])
        }
      }
      return res
    };
    console.log(insert([[1, 3], [6, 9]], [2, 5]))
  </script>
</body>

</html>